﻿namespace Minecraft.Surveyor
{
    using System;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// A dark window theme.
        /// </summary>
        private ResourceDictionary darkTheme;

        /// <summary>
        /// A light window theme.
        /// </summary>
        private ResourceDictionary lightTheme;

        private Color darkGradientTop;
        private Color darkGradientBottom;
        private Color lightGradientTop;
        private Color lightGradientBottom;

        /// <summary>
        /// A value indicating whether the window is currently using the dark window theme.
        /// </summary>
        private bool isDark;

        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.darkTheme = new ResourceDictionary();
            this.darkTheme.Source = new Uri("Themes\\ExpressionDark.xaml", UriKind.Relative);
            this.darkGradientTop = ((LinearGradientBrush)WindowGrid.Background).GradientStops[0].Color;
            this.darkGradientBottom = ((LinearGradientBrush)WindowGrid.Background).GradientStops[1].Color;

            this.lightTheme = new ResourceDictionary();
            this.lightTheme.Source = new Uri("Themes\\ExpressionLight.xaml", UriKind.Relative);
            this.lightGradientTop = Color.FromRgb((byte)(255 - darkGradientBottom.R), (byte)(255 - darkGradientBottom.G), (byte)(255 - darkGradientBottom.B));
            this.lightGradientBottom = Color.FromRgb((byte)(255 - darkGradientTop.R), (byte)(255 - darkGradientTop.G), (byte)(255 - darkGradientTop.B));

            App.Current.Resources.MergedDictionaries.Add(this.darkTheme);
            this.isDark = true;
        }

        /// <summary>
        /// Toggles the dark and light window styles.
        /// </summary>
        /// <param name="sender">The calling object.</param>
        /// <param name="e">Event arguments.</param>
        private void ToggleLamp(object sender, MouseButtonEventArgs e)
        {
            App.Current.Resources.MergedDictionaries.Remove(this.isDark ? this.darkTheme : this.lightTheme);
            App.Current.Resources.MergedDictionaries.Add(this.isDark ? this.lightTheme : this.darkTheme);
            ((LinearGradientBrush)WindowGrid.Background).GradientStops[0].Color = isDark ? this.lightGradientTop : this.darkGradientTop;
            ((LinearGradientBrush)WindowGrid.Background).GradientStops[1].Color = isDark ? this.lightGradientBottom : this.darkGradientBottom;
            this.isDark = !this.isDark;
            ThemeButton.Source = new BitmapImage(new Uri(@"pack://application:,,,/Minecraft.Surveyor;component/Icons/lightbulb" + (this.isDark ? "_off" : string.Empty) + ".png"));
        }
    }
}
